package com.kelvin.paopao.module.main.find.filter;

import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.TagModel;
import com.kelvin.paopao.module.base.IBasePresenter;
import com.kelvin.paopao.widgets.EmptyLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/6/13.
 */

public class FindFilterPresenter implements IBasePresenter {
    private FindFilterActivity mView;

    public FindFilterPresenter(FindFilterActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(final boolean isRefresh) {
        RetrofitService.getLabelInfo().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {

            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<TagModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<TagModel>
                                               commonListResponse) throws Exception {
                        if (!isRefresh) {
                            mView.hideLoading();
                        } else {
                            mView.finishRefresh();
                        }
                        if (commonListResponse.getResult() != null && commonListResponse.getResult().size() != 0) {
                            mView.loadData(commonListResponse.getResult());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        if (!isRefresh) {
                            mView.showNetError(new EmptyLayout.OnRetryListener() {
                                @Override
                                public void onRetry() {
                                    getData(isRefresh);
                                }
                            });
                        } else {
                            ToastUtil.showToast(mView, "网络错误");
                        }
                    }
                });
    }

    @Override
    public void getMoreData() {

    }
}
